//
//  UpVipView.m
//  AiFuGou
//
//  Created by Michael on 16/6/14.
//  Copyright © 2016年 jumper. All rights reserved.
//

#import "UpVipView.h"
@interface UpVipView ()<TXHRrettyRulerDelegate>

@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UIButton  *closeButton;
@property (nonatomic, strong) UIButton  *sureButton;

@property (nonatomic, strong) UIView                *doubleRuleView; ///两个刻度尺
@property (nonatomic, strong) UIView                *grayTiaoView;
@property (nonatomic, strong) UIView                *lineBottom;
@property (nonatomic, strong) UIView                *line;
@property (nonatomic, strong) UIImageView           *pointerImage;
@property (nonatomic, strong) TTTAttributedLabel    *startLabel;    //开始的时间Label
@property (nonatomic, strong) TTTAttributedLabel    *finishLabel;   //结束的时间Label



@property (nonatomic, strong) UIView *indicatorView;  //中间指示针

@end
@implementation UpVipView


- (instancetype) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        
    
        
        self.isSaved = YES;
        
        self.titleView = [UIView new];
        self.titleView.backgroundColor = KHexColor(@"#448AFF");
        [self addSubview:self.titleView];
        
        
        
        [_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(0);
            make.left.equalTo(self.mas_left).offset(0);
            make.width.equalTo([NSNumber numberWithInt:CoreWidth]);
            make.height.equalTo([NSNumber numberWithInt:60]);
        }];

        
        self.titleLabel = [TTTAttributedLabel new];
        self.titleLabel.font = KFontSize(14);
        self.titleLabel.textColor = KHexColor(@"#ffffff");
        //self.titleLabel.text = @"来电提醒";
        [self.titleView addSubview:_titleLabel];
        
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.titleView.mas_centerX).offset(0);
            make.bottom.equalTo(self.titleView.mas_bottom).offset(-10);
            make.width.equalTo(self.titleLabel.mas_width);
            make.height.equalTo([NSNumber numberWithInt:14]);
        }];

      
        
        
        self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.closeButton.titleLabel.font = KFontSize(14);
        [self.closeButton setTitle:HHH_Text(@"取消") forState:UIControlStateNormal];
        [self.closeButton setTitleColor:KHexColor(@"#ffffff") forState:UIControlStateNormal];
        [self.closeButton addTarget:self action:@selector(closeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleView addSubview:_closeButton];
        
        [_closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleView.mas_top).offset(0);
            make.left.equalTo(self.titleView.mas_left).offset(0);
            make.width.equalTo([NSNumber numberWithInt:80]);
            make.height.equalTo([NSNumber numberWithInt:40]);
        }];
        
        NSLog(@"closeButton = %p",self.closeButton);
        
        self.sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.sureButton.titleLabel.font = KFontSize(14);
        [self.sureButton setTitle:HHH_Text(@"确定") forState:UIControlStateNormal];
        [self.sureButton setTitleColor:KHexColor(@"#ffffff") forState:UIControlStateNormal];
        [self.sureButton addTarget:self action:@selector(sureButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleView addSubview:_sureButton];
        
        [_sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleView.mas_top).offset(0);
            make.right.equalTo(self.titleView.mas_right).offset(0);
            make.width.equalTo([NSNumber numberWithInt:80]);
            make.height.equalTo([NSNumber numberWithInt:40]);
        }];
        
        [self makeDoubleRuler];
        
    }
    return self;
}

- (void)closeButtonClick:(UIButton *)button
{
    
    if (self.dismissKuangBlock) {
        self.dismissKuangBlock();
    }
}
- (void)sureButtonClick:(UIButton *)button
{
    
    
    
    if (self.sureKuangBlock)
    {
        self.sureKuangBlock();
       
      
    }
}


- (void)makeDoubleRuler
{
    self.doubleRuleView = [UIView new];
    [self addSubview:_doubleRuleView];
    
    [_doubleRuleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(-40);
        make.left.equalTo(self.mas_left).offset(0);
        make.width.equalTo([NSNumber numberWithInt:(CoreWidth-40)]);
        make.height.equalTo([NSNumber numberWithInt:130]);
    }];

    self.indicatorView = [UIView new];
    self.indicatorView.backgroundColor = KHexColor(@"#448AFF");
    [self addSubview:_indicatorView];
    
    
    [_indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX).offset(0);
        make.top.equalTo(self.doubleRuleView.mas_top).offset(40);
        make.bottom.equalTo(self.doubleRuleView.mas_bottom).offset(-40);
        make.width.equalTo([NSNumber numberWithInt:1]);
    }];

    
    
    self.line = [UIView new];
    self.line.backgroundColor = KHexColor(@"#dbdbdb");
    [self.doubleRuleView addSubview:_line];
    
    
    
    self.lineBottom = [UIView new];
    self.lineBottom.backgroundColor = KHexColor(@"#dbdbdb");
    [self.doubleRuleView addSubview:_lineBottom];
    
    
    self.grayTiaoView = [UIView new];
    self.grayTiaoView.layer.borderColor = KHexColor(@"#cecece").CGColor;
    self.grayTiaoView.backgroundColor = KHexColor(@"#e9e9e9");
    [self.doubleRuleView addSubview:_grayTiaoView];
    
    
    
    //两个刻度尺的创建
    self.rulerDown = [[TXHRrettyRulerOM alloc] initWithFrame:CGRectMake(20, 90, [UIScreen mainScreen].bounds.size.width - 20 * 2, 50)];
    self.rulerDown.rulerDelegate = self;
    [self.rulerDown showRulerScrollViewWithMix:0 Max:60 currentValue:10 rulerDirection:RulerDirectionType_Down];
    [self.doubleRuleView addSubview:self.rulerDown];
    
    
    
    self.rulerUp = [[TXHRrettyRulerOM alloc] initWithFrame:CGRectMake(20, 10, [UIScreen mainScreen].bounds.size.width - 20 * 2, 50)];
    self.rulerUp.rulerDelegate = self;
    [self.rulerUp showRulerScrollViewWithMix:0 Max:60 currentValue:0 rulerDirection:RulerDirectionType_Up];
    [self.doubleRuleView addSubview:self.rulerUp];
    
    self.pointerImage = [UIImageView new];
    self.pointerImage.image = [UIImage imageNamed:@"blood_pointer"];
    [self.doubleRuleView addSubview:_pointerImage];
    
    self.startLabel = [TTTAttributedLabel new];
    self.startLabel.textColor = KHexColor(@"#9f9f9f");
    self.startLabel.font = KFontSize(12);
    self.startLabel.text = [NSString stringWithFormat:@"第%@开始",@"0"];
    [self.doubleRuleView addSubview:_startLabel];
    
//    [self.ChishouSuoYaLabel setText:@"收缩压" afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
//        
//        NSAttributedString *yuan = [[NSAttributedString alloc] initWithString:@"(mmHg)"
//                                                                   attributes:@{NSForegroundColorAttributeName : KHexColor(@"#dbdbdb"),
//                                                                                NSFontAttributeName:KFontSize(12)}];
//        [mutableAttributedString appendAttributedString:yuan];
//        return mutableAttributedString;
//    }];
    
    self.finishLabel = [TTTAttributedLabel new];
    self.finishLabel.textColor = KHexColor(@"#9f9f9f");
    self.finishLabel.font = KFontSize(12);
    self.finishLabel.text = [NSString stringWithFormat:@"第%@结束",@"10"];
    [self.doubleRuleView addSubview:_finishLabel];
    
    
//    [self.ChiShuZhangYaLabel setText:@"舒张压" afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
//        
//        NSAttributedString *yuan = [[NSAttributedString alloc] initWithString:@"(mmHg)"
//                                                                   attributes:@{NSForegroundColorAttributeName : KHexColor(@"#dbdbdb"),
//                                                                                NSFontAttributeName:KFontSize(12)}];
//        [mutableAttributedString appendAttributedString:yuan];
//        return mutableAttributedString;
//    }];
    
    
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.doubleRuleView.mas_top).offset(0);
        make.left.equalTo(self.doubleRuleView.mas_left).offset(20);
        make.width.equalTo([NSNumber numberWithInt:CoreWidth -40]);
        make.height.equalTo([NSNumber numberWithFloat:0.5]);
    }];
    [_lineBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.doubleRuleView.mas_bottom).offset(0);
        make.left.equalTo(self.doubleRuleView.mas_left).offset(20);
        make.width.equalTo([NSNumber numberWithInt:CoreWidth -40]);
        make.height.equalTo([NSNumber numberWithFloat:0.5]);
    }];
    [_grayTiaoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.doubleRuleView.mas_centerY).offset(0);
        make.left.equalTo(self.doubleRuleView.mas_left).offset(20);
        make.width.equalTo([NSNumber numberWithInt:CoreWidth -40]);
        make.height.equalTo([NSNumber numberWithInt:10]);
    }];
    [_rulerDown mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.grayTiaoView.mas_bottom).offset(0);
        make.left.equalTo(self.doubleRuleView.mas_left).offset(20);
        make.width.equalTo([NSNumber numberWithInt:CoreWidth - 40]);
        make.height.equalTo([NSNumber numberWithInt:50]);
    }];
    
    [_rulerUp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.grayTiaoView.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(20);
        make.width.equalTo([NSNumber numberWithInt:CoreWidth - 40]);
        make.height.equalTo([NSNumber numberWithInt:50]);
    }];
    [_pointerImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.grayTiaoView.mas_centerY).offset(0);
        make.centerX.equalTo(self.grayTiaoView.mas_centerX).offset(0);
        make.width.equalTo([NSNumber numberWithFloat:(14/4 *3)]);
        make.height.equalTo([NSNumber numberWithFloat:(28/4 *3)]);
    }];
    [_startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line.mas_top).offset(5);
        make.left.equalTo(self.doubleRuleView.mas_left).offset(20);
        make.width.equalTo(self.startLabel.mas_width);
        make.height.equalTo([NSNumber numberWithInt:12]);
    }];
    [_finishLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.lineBottom.mas_bottom).offset(-5);
        make.left.equalTo(self.doubleRuleView.mas_left).offset(20);
        make.width.equalTo(self.finishLabel.mas_width);
        make.height.equalTo([NSNumber numberWithInt:12]);
    }];
}


#pragma mark
#pragma mark RulerDelegate
- (void)rulerDidScroll:(TXHRrettyRulerOM *)ruler withValue:(CGFloat)rulerValue
{
    
    
    NSLog(@"rulerValue = %f",rulerValue);
    if (ruler == _rulerUp)
    {
        self.startLabel.text = [NSString stringWithFormat:@"第%1.fs开始",rulerValue];
        [self start:rulerValue finish:[self.finishLabel.text intValue]];
        
        self.startSring = [NSString stringWithFormat:@"%1.f",rulerValue]; //赋值   0
        if (_rulerDown.rulerScrollView.contentOffset.x < _rulerUp.rulerScrollView.contentOffset.x) {
            [_rulerDown.rulerScrollView setContentOffset:CGPointMake(_rulerUp.rulerScrollView.contentOffset.x, 0)];
        }
        //移动上面的尺子(up)的时候   下面尺子(down)就要移动的刻度比上面多十个刻度；
        
        
    }
    else if (ruler == _rulerDown)
    {
        self.finishLabel.text = [NSString stringWithFormat:@"到%1.fs结束",rulerValue];
        self.finishedSring = [NSString stringWithFormat:@"%1.f",rulerValue]; //赋值 10

        [self start:[self.startLabel.text intValue] finish:rulerValue];
        if (_rulerDown.rulerScrollView.contentOffset.x< _rulerUp.rulerScrollView.contentOffset.x) {
            [_rulerUp.rulerScrollView setContentOffset:CGPointMake(_rulerDown.rulerScrollView.contentOffset.x, 0)];
        }
        
        //移动下面尺子(down)的时候   上面的尺子(up)就要移动的刻度比上面十个刻度；xx

    }
}

- (void)start:(CGFloat)startS finish:(CGFloat)finishS
{
    
    
    if ([[NSString stringWithFormat:@"%1.f",finishS] integerValue] - [[NSString stringWithFormat:@"%1.f",startS] integerValue] >= 10)
    {
        self.isSaved = YES;
    }
    else
    {
        self.isSaved = NO;
    }
}


@end
